﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Routing;
using VQWEB.administrator.sources;
using VQData;
using System.Data;

namespace VQWEB.administrator.membership
{
    public partial class user_add : System.Web.UI.Page
    {
        MembershipData MembershipData = new MembershipData();
        core_user users = new core_user();
        int Id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (RouteData.Values["Id"] != null)
                int.TryParse(RouteData.Values["Id"].ToString(), out Id);
            if (Id > 0)
            {
                frmUsers.DefaultMode = FormViewMode.Edit;
                users = MembershipData.GetUsersById(Id);
                frmUsers.DataSource = new List<core_user> { users };
                frmUsers.DataBind();
            }
            else
                frmUsers.DefaultMode = FormViewMode.Insert;

        }

        protected void frmUsers_ItemCreated(object sender, EventArgs e)
        {
            var control = frmUsers.Row.FindControl("ltrRoleId") as Literal;
            string html = "<select  class=\"form-control\" name=\"role_id\">";
            html += "<option value=\"0\"> Lựa chọn vai trò </option>";
            int role_id = (users != null && users.role_id != null) ? users.role_id.Value : 0;

            DataTable table = MembershipData.ViewRoles();
            foreach (DataRow item in table.Rows)
            {
                string selected = string.Empty;
                int rId = (int)item["role_id"];
                string rName = (string)item["role_name"];
                if (role_id == rId)
                    selected = " selected=\"selected\" ";
                html += "<option " + selected + " value=" + rId + ">" + rName + "</option>";

            }
            html += "</select>";
            control.Text = html;
        }       
    }
}